﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Configuration;

namespace PostSubmit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txt_Url.SelectedIndex = 0;
        }

        //发送请求
        private void btn_post_Click(object sender, EventArgs e)
        {
            try
            {
                string str_port = "80";
                string pram = string.Empty;
                pram = txt_post.Text;
                str_port = txt_port.Text;
                string url = string.Format("{0}:{1}/{2}", txt_Url.Text, str_port, txt_pageUrl.Text);
                string es = string.Empty;
                if (radioButton1.Checked)
                {
                    es = "gb2312";
                }
                if (radioButton2.Checked)
                {
                    es = "utf-8";
                }
                string result = HttpRequestHelper.GetRequest(pram, url, es);
                txt_back.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作异常：" + ex);
            }
        }

        private void cmb_directory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList arrayList = GetDirectories(@"F:\TCnew\FlightAdminService\Flight.AdminService.API");
            txt_pageUrl.DataSource = arrayList;
        }


        #region 获取指定目录下的文件路径
        /// <summary>
        /// 获取指定目录下的文件路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ArrayList FileList(string path)
        {
            ArrayList arrayList = new ArrayList();
            string[] strFile = Directory.GetDirectories(path);
            if (strFile.Length == 0)
            {
                return new ArrayList();
            }
            foreach (string file in strFile)
            {
                arrayList.Add(file.Replace(path, "").Replace("/", @"\"));
            }
            return arrayList;
        }
        #endregion

        private ArrayList GetDirectories(string path)
        {
            ArrayList arrayList = new ArrayList();
            string[] directories = Directory.GetDirectories(path);
            foreach (string dir in directories)
            {
                string[] fileNames = Directory.GetFiles(dir);
                foreach (string file in fileNames)
                {
                    if (file.Split('.')[file.Split('.').Length - 1] == "ashx")
                    {
                        arrayList.Add(file.Replace(path, "").Replace('\\', '/').TrimStart('/'));
                    }
                }
                GetDirectories(dir);
            }
            return arrayList;
        }
        ////调用：
        //getDirectories(@"f:\") ;


        /// <summary>
        /// 获取文件类型
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public FileType GetFileType(string file)
        {
            FileType type = FileType.dll;

            try
            {
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                byte[] buff = new byte[2];
                fs.Read(buff, 0, 2);
                string b = buff[0].ToString() + buff[1].ToString();
                type = (FileType)Enum.Parse(typeof(FileType), b);
            }
            catch (Exception)
            {

            }
            return type;
        }

        private void txt_pageUrl_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index <= 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(txt_pageUrl.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Brown), e.Bounds.X, e.Bounds.Y + 3);
        }
    }

    public enum FileType
    {
        //7790:exe,8297:rar,8075:zip,255216:jpg,8297:rar,7173:gif,6677:bmp,13780:png,7784:midi
        /// <summary>
        /// 
        /// </summary>
        exe = 7790,
        /// <summary>
        /// 
        /// </summary>
        rar = 8297,
        /// <summary>
        /// 
        /// </summary>
        zip = 8075,
        /// <summary>
        /// 
        /// </summary>
        jpg = 255216,
        /// <summary>
        /// 
        /// </summary>
        gif = 7173,
        /// <summary>
        /// 
        /// </summary>
        bmp = 6677,
        /// <summary>
        /// 
        /// </summary>
        png = 13780,
        /// <summary>
        /// 
        /// </summary>
        midi = 7784,
        /// <summary>
        /// 
        /// </summary>
        dll = 7790
    }
}
